package com.sskj.fca.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.TipMineUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.fca.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by QinKang on 2019/1/1.
 */

public class MainPopup {

    private MaterialDialog loginTip;
    private PopupWindow popupWindow;
    private RecyclerView recyclerView;
    private LayoutInflater layoutInflater;
    private Context mContext;

    private MainPopup mySelectPopupWindow;

    public MainPopup(final Context context, UserData userData) {
        this.mContext = context;
        if (popupWindow == null) {
            layoutInflater = LayoutInflater.from(context);
            final View popupView = layoutInflater.inflate(R.layout.app_popup_main, null);
            recyclerView = popupView.findViewById(R.id.recyclerView_popup);
            ArrayList<String> mainMenuBeans = new ArrayList<>();
            mainMenuBeans.add("用户信息");
            mainMenuBeans.add("我的资产");
            mainMenuBeans.add("安全设置");
            mainMenuBeans.add("支付密码");
            mainMenuBeans.add("钱包地址");
            mainMenuBeans.add(userData == null ? "登录" : "退出");
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerView.addItemDecoration(new DividerLineItemDecoration(mContext)
                    .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                    .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
            );
            SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_main_popup, new SlimInjector<String>() {
                @Override
                public void onInject(String data, IViewInjector injector, List payloads) {
                    injector.text(R.id.tv_popup, data);
                }
            }).attachTo(recyclerView).updateData(mainMenuBeans);
            ItemClickSupport.addTo(recyclerView)
                    .setOnItemClickListener((recyclerView1, position, v) -> {
                        if (position != 5 && userData == null) {
                            if (loginTip == null) {
                                loginTip = TipMineUtil.getTip(context, "请先登录", "去登录", () -> {
                                    ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                                });
                            }
                            loginTip.show();
                            return;
                        }
                        popupWindow.dismiss();
                        switch (position) {
                            case 0://用户信息
                                ARouter.getInstance().build(RConfig.APP_USER_INFO).navigation();
                                break;
                            case 1://我的资产
                                ARouter.getInstance().build(RConfig.APP_ASSET).navigation();
                                break;
                            case 2://安全设置
                                ARouter.getInstance().build(RConfig.APP_SAFE_SETTING).navigation();
                                break;
                            case 3://支付密码
                                ARouter.getInstance().build(RConfig.APP_SET_PAY_PWD).navigation();
                                break;
                            case 4://钱包地址
                                ARouter.getInstance().build(RConfig.APP_WALLET_ADDRESS_MANAGER).navigation();
                                break;
                            case 5://退出
                                if (userData == null) {
                                    ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                                } else {
                                    LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
                                    logoutProvider.logout();
                                }
                                break;
                        }
                    });

            popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            popupWindow.setAnimationStyle(R.style.popwindow_anim);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);
            popupWindow.setOutsideTouchable(true);
        }
    }

    public void setMySelectPopupWindow(MainPopup mySelectPopupWindow) {
        this.mySelectPopupWindow = mySelectPopupWindow;
    }

    public void setBackgroundAlpha(float bgAlpha, Context mContext) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    public void show(View view) {
        setBackgroundAlpha(1.0f, mContext);
        popupWindow.showAsDropDown(view, 0, 0, Gravity.RIGHT);
    }


}
